package com.wjy.sfhcore.itf.impl;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import com.wjy.sfhcore.ui.activity.CoreActivity;


/**
 * Activity页面跳转
 * 
 * @author wjy
 * 
 */
public class ActivityIntentFactory extends IntentFactory
{
	private FragmentActivity activity;

	public ActivityIntentFactory(FragmentActivity activity)
	{
		super(activity);
		this.activity = activity;
	}

	private FragmentTransaction initFragmentTransaction()
	{
		return activity.getSupportFragmentManager().beginTransaction();
	}

	public void addFragment(int containerId,Fragment fragment)
	{
		FragmentTransaction transaction = initFragmentTransaction();
		transaction.add(containerId, fragment);
		transaction.commitAllowingStateLoss();
	}

	public void replaceFragment(int containerId,Fragment newFragment)
	{
		replaceFragment(containerId, newFragment, true);
	}


	public void replaceFragment(int contentID, Fragment newFragment,
	        boolean addToBackStack)
	{
		FragmentTransaction transaction = initFragmentTransaction();
		// transaction.setCustomAnimations(R.anim.push_left_in,
		// R.anim.push_left_out, R.anim.push_right_in,
		// R.anim.push_right_out);
		transaction.replace(contentID, newFragment);
		if (addToBackStack)
		{
			transaction.addToBackStack(null);
		}
		transaction.commitAllowingStateLoss();
	}


	public void goToOthersForResult(Class<?> cls, Bundle bundle, int requestCode)
	{
		Intent intent = new Intent(activity, cls);
		intent.putExtra(CoreActivity.PARAM_INTENT, bundle);
		activity.startActivityForResult(intent, requestCode);
	}

	public void backForResult(Class<?> cls, Bundle bundle, int resultCode)
	{
		Intent intent = new Intent();
		if (cls != null)
		{
			intent.setClass(activity, cls);
		}
		intent.putExtra(CoreActivity.PARAM_INTENT, bundle);
		activity.setResult(resultCode, intent);
		activity.finish();
	}
}
